#include "utils.h"

std::string getNowTime()
{
    time_t nowtime;
	struct tm* p;;
	time(&nowtime);
	p = localtime(&nowtime);
    std::string res;
    res.append(std::to_string(p->tm_hour) + ":" + std::to_string(p->tm_min) + ":" + std::to_string(p->tm_sec) + " ");
	return res;

}

void moveCursor(char direction,int m)
{
    if(direction == 'u') printf("\033[%dA", m);
    if(direction == 'd') printf("\033[%dB", m);
    if(direction == 'r') printf("\033[%dC", m);
    if(direction == 'l') printf("\033[%dD", m);
}

std::string readCommand()
{
    std::string command;
    char c;
    int count = 0;
    std::string tempcommand = "";
    while ((c = getchar()) != '\n' || count == 0)
    {
        if (c != '\n')
            tempcommand.push_back(c);
        ++count;
    }
    purifyCommand(&tempcommand);
    return tempcommand;
}

void purifyCommand(std::string *command)
{
    if(*command == "") return;
    int b, e;
    for (int i = 0; i < command->size(); i++)
    {
        if ((*command)[i] != ' ')
        {
            b = i;
            break;
        }
    }
    for (int i = command->size() - 1; i >= 0; i--)
    {
        if ((*command)[i] != ' ')
        {
            e = i;
            break;
        }
    }
    *command = command->substr(b, e - b + 1);
}

std::vector<std::string> split(std::string src, char c)
{
    std::vector<std::string> result;
    int b = 0;
    bool isok = 1;
    for (int i = 0; i < src.size(); i++)
    {
        if (src[i] == '\"')
        {
            isok = (isok) ? 0 : 1;
        }
        if ((src[i] == c) && isok)
        {
            if (src[b] != '\"')
                result.push_back(src.substr(b, i - b));
            else
                result.push_back(src.substr(b + 1, i - b - 2));
            b = i + 1;
        }
    }
    if(src[b] == '\"') result.push_back(src.substr(b + 1 , src.size() - b - 2));
    else result.push_back(src.substr(b , src.size() - b));
    return result;
}

std::vector< std::vector<std::string> > readFile(std::string filename)
{
    std::fstream file;

    if(filename == LG_PATH)
    {
        int index = 0;
        file.open(LG_PATH, std::ios::in);
        std::vector< std::vector<std::string> > ret(4);
        ret[0].push_back("SN");
        ret[1].push_back("User name");
        ret[2].push_back("Password");
        ret[3].push_back("User ID");
        while(!file.eof())
        {
            std::string line;
            std::getline(file, line);
            if(line.empty()) continue;
            std::vector< std::string > temp = split(line, ' ');
            for(int i = 0; i < temp.size(); i++) ret[i + 1].push_back(temp[i]);
            ret[0].push_back(std::to_string(++index));
        }
        file.close();
        return ret;
    }
    else if(filename == STU_PATH)
    {
        int index = 0;
        file.open(STU_PATH, std::ios::in);
        std::vector< std::vector<std::string> > ret(6);
        ret[0].push_back("SN");
        ret[1].push_back("Student ID");
        ret[2].push_back("Student Name");
        ret[3].push_back("Sexual");
        ret[4].push_back("Subjects");
        ret[5].push_back("Credits");
        while(!file.eof())
        {
            std::string line;
            std::getline(file, line);
            if(line.empty()) continue;
            std::vector< std::string > temp = split(line, ' ');
            for(int i = 0; i < temp.size(); i++) ret[i + 1].push_back(temp[i]);
            ret[0].push_back(std::to_string(++index));
        }
        file.close();
        return ret;
    }
    else
    {
        int index = 0;
        file.open(SUB_PATH, std::ios::in);
        
        std::vector< std::vector<std::string> > ret(7);
        ret[0].push_back("SN");
        ret[1].push_back("Subject Title");
        ret[2].push_back("Subject Nature");
        ret[3].push_back("Total Classes");
        ret[4].push_back("Credits");
        ret[5].push_back("Selected");
        ret[6].push_back("Remained");
        while(!file.eof())
        {
            std::string line;
            std::getline(file, line);
            if(line.empty()) continue;
            std::vector< std::string > temp = split(line, ' ');
            for(int i = 0; i < temp.size(); i++) ret[i].push_back(temp[i]);
        }
        file.close();

        return ret;
    }
}

int isDigit(std::string str)
{
    int ret = 0;
    for(int i = 0; i < str.size(); i++)
    {
        if(!(str[i] >= '0' && str[i] <= '9')) return -1;
        else
        {
            int tmp = (str[i] - '0');
            ret += tmp * pow(10, str.size() - 1 - i);
        }
    }
    return ret;
}